<template>
    <page-view :crumbs-data='crumbsData'>
        <h-card fixed>
            <h-edit-table
                fixed
                fixedCheck
                isFullScreen
                ref='tableRef'
                :columns='columns'
                :data='loadData'
                :rowSelection='{ selectedRowKeys, onSelect }'>
                    <template slot='operator'>
                        <a-button size='small' type='primary' icon='plus' @click="handleAdd('add')">
                            新增
                        </a-button>
                        <a-button
                                type='ghost-warning'
                                size='small'
                                icon='download'
                                @click="handleExportXls({},'${tableTxt}')"
                        >
                            导出
                        </a-button>
                        <a-button size='small' type='ghost-success' icon='import' @click="handleImportExcel('${tableTxt}')">导入
                        </a-button>
                        <a-popconfirm v-if='selectedRowKeys.length' title='确定删除吗?' @confirm='handleBatchDelete'>
                            <a-button size='small' type='danger' ghost icon='delete'>
                                批量删除
                            </a-button>
                        </a-popconfirm>
                    </template>
                    <a-space slot='actions' slot-scope='text, record'>
                        <a @click="handleEdit(record, 'edit')"> 编辑 </a>
                        <a-popconfirm title='确定删除吗?' @confirm='handleDelete(record.id)'>
                            <a class='danger-text'>删除</a>
                        </a-popconfirm>
                    </a-space>
            </h-edit-table>
            <h-file-import
                    ref='HFileImport'
                    :isHistory='false'
                    @change='refresh'
                    @downloadExcel='handleExportXls({}, "${tableTxt}", "template")'
            />
            <${webPrefixMiddle}-modal ref='modalRef' @change='refresh' />
        </h-card>
    </page-view>
</template>

<script>
    import PageView from '@/views/components/layouts/PageView.vue';
    import ExportImportMixin from '@/views/hifar/ExportImportMixin';
    import { postAction } from '@/api/manage';
    import ${webPrefix}Modal from './${webPrefix}Modal.vue';
    import { tableColumns } from './data';

    export default {
        name: '${webPrefix}List',
        components: { ${webPrefix}Modal, PageView},
        mixins: [ExportImportMixin],
        data() {
            return {
                crumbsData: [
                    {
                        title: '${tableTxt}',
                        icon: 'appstore',
                        currentRoute: true,
                        rollback: false
                    }
                ],
                selectedRows: [],
                selectedRowKeys: [],
                columns: tableColumns,
                loadData: (params) => {
                    let data = {
                        ...params
                    };
                    this.selectedRows = [];
                    this.selectedRowKeys = [];
                    return postAction(this.url.list, data).then((res) => {
                        if (res.code === 200) return res.data;
                    });
                },
                url: {
                    list: '/${webPrefix}Controller/list', // 带分页list
                    listAll: '/${webPrefix}Controller/listAll', // 不带分页list
                    delete: '/${webPrefix}Controller/delete',
                    export: '/${webPrefix}Controller/exportExcel',
                    import: '/${webPrefix}Controller/importExcel'
                }
            };
        },
        methods: {
            refresh() {
                this.selectedRowKeys = [];
                this.selectedRows = [];
                this.$refs.tableRef.refresh();
            },
            handleBatchDelete() {
                if (!this.selectedRowKeys.length) return this.$message.warning('请至少选择一条数据');
                this.handleDelete(this.selectedRowKeys.join(','));
            },
            handleDelete(id) {
                this.deleteRequest(id);
            },
            deleteRequest(id) {
                postAction(this.url.delete, { id }).then((res) => {
                    const { code = 0 } = res;
                    if (code === 200) {
                        this.$message.success('删除成功');
                        this.refresh();
                    }
                });
            },
            handleEdit(record, type) {
                this.$refs.modalRef.show(record, type);
            },
            handleAdd(type) {
                this.$refs.modalRef.show({}, type);
            },
            onSelect(selectedRowKeys, selectedRows) {
                this.selectedRowKeys = selectedRowKeys;
                this.selectedRows = selectedRows;
            }
        }
    };
</script>

<style scoped lang='less'>

</style>